package com.DAO;

import com.Module.MenberU;
import com.Util.ModuleIte;
import com.Util.Sql;
import com.Module.Menber;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class Menberinfo implements Sql {
    public List<Menber> list=new ArrayList<>();
    @Override
    public void select() {
        DatabaseConnection databaseConnection=new DatabaseConnection();
        try {
            Connection connection=databaseConnection.getconn("hotel","root","");
            Statement statement=connection.createStatement();
            String sql="select * from  menber";
            ResultSet resultSet=statement.executeQuery(sql);
            while (resultSet.next()){
                 int mid=resultSet.getInt("mid");
                 String mname=resultSet.getString("mname");
                 String mnumber =resultSet.getString("mnumber");
                 int roomid=resultSet.getInt("roomid");
                 double rentmoney=resultSet.getDouble("rentmoney");
                 String indate=resultSet.getString("indate");
                 String enddate=resultSet.getString("enddate");
                 double payment=resultSet.getDouble("payment");
                 Menber menber=new Menber(mid,mname,mnumber,roomid,rentmoney,indate,enddate,payment);
                 list.add(menber);
            }
            databaseConnection.release(connection,statement,resultSet);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public List<Object> select(String id) {
        return null;
    }

    @Override
    public int update() {
        return 0;
    }

    @Override
    public int update(Object obj) {
        DatabaseConnection databaseConnection=new DatabaseConnection();
        int ans=0;
        try {
            Connection connection=databaseConnection.getconn("hotel","root","");
            Statement statement=connection.createStatement();
            MenberU menberU=(MenberU) obj;
            String sql="insert into menber values ("+menberU.getId()+",'"+menberU.getName()+"','"+menberU.getNumber()+"',"+menberU.getRoomid()+",50.00,'"+menberU.getTime()+"',null,300)";
            ans=statement.executeUpdate(sql);
            databaseConnection.release(connection,statement);
        }catch (Exception e){
            e.printStackTrace();
        }
        return ans;
    }

    @Override
    public int delete(Object obj) {
        DatabaseConnection databaseConnection=new DatabaseConnection();
        int ans = 0;
        try {
            Connection connection=databaseConnection.getconn("hotel","root","");
            Statement statement=connection.createStatement();
            MenberU menberU=(MenberU) obj;
            ModuleIte moduleIte=new ModuleIte();
            String sql="UPDATE  menber SET enddate='"+moduleIte.gettime()+"' where mid="+menberU.getId();
            ans=statement.executeUpdate(sql);
            databaseConnection.release(connection,statement);
        }catch (Exception e){
            e.printStackTrace();
        }
        return ans;
    }

    @Override
    public int modify(Object obj) {
        return 0;
    }

}
